package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ProPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WxMaterMapper;
import com.ruoyi.system.domain.WxMater;
import com.ruoyi.system.service.IWxMaterService;
import com.ruoyi.common.core.text.Convert;

/**
 * materService业务层处理
 *
 * @author ruoyi
 * @date 2021-07-31
 */
@Service
public class WxMaterServiceImpl implements IWxMaterService {
    @Autowired
    private WxMaterMapper wxMaterMapper;

    /**
     * 查询mater
     *
     * @param materId materID
     * @return mater
     */
    @Override
    public WxMater selectWxMaterById(Long materId) {
        return wxMaterMapper.selectWxMaterById(materId);
    }

    /**
     * 查询mater列表
     *
     * @param wxMater mater
     * @return mater
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<WxMater> selectWxMaterList(WxMater wxMater) {
        return wxMaterMapper.selectWxMaterList(wxMater);
    }

    /**
     * 新增mater
     *
     * @param wxMater mater
     * @return 结果
     */
    @Override
    public int insertWxMater(WxMater wxMater) {
        return wxMaterMapper.insertWxMater(wxMater);
    }

    /**
     * 修改mater
     *
     * @param wxMater mater
     * @return 结果
     */
    @Override
    public int updateWxMater(WxMater wxMater) {
        return wxMaterMapper.updateWxMater(wxMater);
    }

    /**
     * 删除mater对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWxMaterByIds(String ids) {
        return wxMaterMapper.deleteWxMaterByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除mater信息
     *
     * @param materId materID
     * @return 结果
     */
    @Override
    public int deleteWxMaterById(Long materId) {
        return wxMaterMapper.deleteWxMaterById(materId);
    }

    @Override
    public List<SysDept> selectAssDeptsByMaterId(Long materId) {
        return wxMaterMapper.selectAssDeptsByMaterId(materId);
    }

    @Override
    public String checkPhoneUnique(WxMater wxMater) {
        Long materId = StringUtils.isNull(wxMater.getMaterId()) ? -1L : wxMater.getMaterId();
        WxMater mater = wxMaterMapper.checkPhoneUnique(wxMater.getMaterPhone());
        if (StringUtils.isNotNull(mater) && mater.getMaterId().longValue() != materId.longValue()) {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }

    @Override
    public List<SysDept> selectDeptsByMaterId(Long materId) {
        return wxMaterMapper.selectDeptsByMaterId(materId);
    }

    @Override
    public List<WxMater> selectWxMaterLists(Map map) {
        return wxMaterMapper.selectWxMaterLists(map);
    }

    @Override
    public int saveAssProgram(WxMater wxMater) {
        if(StringUtils.isNotEmpty(wxMater.getAssProgramId())){
            String[] assProgramIdArr = wxMater.getAssProgramId().split(",");
            List<ProPermission> proPermissions = new ArrayList<>();
            for (String assProgramId : assProgramIdArr) {
                ProPermission proPermission = new ProPermission();
                proPermission.setAssDeptId(assProgramId);
                proPermission.setMaterId(wxMater.getMaterId());
                proPermissions.add(proPermission);
            }
            return wxMaterMapper.saveProPermissionBatch(proPermissions);
        }else{
            return 1;
        }
    }

    @Override
    public int deleteProPermissionByMaterId(Long materId) {
        return wxMaterMapper.deleteProPermissionByMaterId(materId);
    }

    @Override
    public List<SysDept> selectDeptsByMaterPhone(String materPhone) {
        return wxMaterMapper.selectDeptsByMaterPhone(materPhone);
    }
}
